package com.imau.controller;

import com.imau.bean.SysDept;
import com.imau.common.entiry.ResultCode;
import com.imau.common.entiry.ResultEntity;
import com.imau.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.List;

@RequestMapping("/sysdept")
@RestController
public class SysdeptConterllor {

    @Autowired
    private SysDeptService sysdeptService;


    @PostMapping
    public ResultEntity add(@RequestBody SysDept sysDept) {


        //3.保存
        boolean b = sysdeptService.saveDept(sysDept);

        //4.返回响应

        return ResultEntity.writeResult(b);

    }


    @DeleteMapping("/{did}")

    public ResultEntity removeById(@PathVariable String did) {


        //删除
        boolean b = sysdeptService.removeDeptById(did);
        //4.返回响应

        return ResultEntity.writeResult(b);


    }

    @PutMapping
    public ResultEntity edit(@RequestBody SysDept dept) {


        boolean b = sysdeptService.editDept(dept);

        return ResultEntity.writeResult(b);
    }


    /**
     * @throws ServletException
     * @throws IOException
     */
    @GetMapping
    public ResultEntity list( SysDept dept) throws ServletException, IOException {


        List<SysDept> depts = sysdeptService.queryListDept(dept);


        return  new ResultEntity(ResultCode.SUCCESS,depts);

    }



    @GetMapping("/{did}")

    public ResultEntity getInfo(@PathVariable String did )  {



        SysDept SysDept = sysdeptService.queryById(did);

        return new ResultEntity(ResultCode.SUCCESS,SysDept);



    }

}
